//
//  ImageScrollView.m
//  SohoNaru
//
//  Created by ladaka31 ladaka on 13. 5. 20..
//  Copyright (c) 2013년 ladaka31 ladaka. All rights reserved.
//

#import "ImageScrollView.h"
#import <QuartzCore/QuartzCore.h>

@implementation ImageScrollView

@synthesize imageView;

- (id)initWithFrame:(CGRect)frame setImagePath:(NSString*)path
{
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    
    if (self) {
        
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        // Initialization code
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
        [self addSubview:imageView];
     
        CGSize imageSize = imageView.frame.size;
        CGSize maxSize = self.frame.size;
        CGFloat widthRadio = maxSize.width / imageSize.width;
        CGFloat heightRatio = maxSize.height / imageSize.height;
        CGFloat initalZoom = (widthRadio > heightRatio) ? widthRadio : heightRatio;
        
        self.delegate = self;
        self.minimumZoomScale = initalZoom;;
        self.maximumZoomScale = 1.0f;
        self.contentSize = imageSize;
        self.zoomScale = initalZoom;
        
        [self setScrollZoom:NO];
    }
  
    return self;
}


- (void) awakeFromNib {
    self.bounces = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    CGSize imageSize = imageView.frame.size;
    CGSize maxSize = self.frame.size;
    CGFloat widthRadio = maxSize.width / imageSize.width;
    CGFloat heightRatio = maxSize.height / imageSize.height;
    CGFloat initalZoom = (widthRadio > heightRatio) ? widthRadio : heightRatio;
    
    self.delegate = self;
    self.minimumZoomScale = initalZoom;;
    self.maximumZoomScale = 1.0f;
    self.contentSize = imageSize;
    self.zoomScale = initalZoom;
    
    [self setScrollZoom:NO];
}
 
- (void)setImageChange:(UIImage*) image{
    [imageView setImage:image];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
     NSLog(@"ImageScrollView touchesBegan");
    
    if(!isEditMode)
    {
       return;
    }
        
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
        if (self.zoomScale == self.minimumZoomScale)
            [self setZoomScale:self.maximumZoomScale animated:YES];
        else if (self.zoomScale == self.maximumZoomScale)
            [self setZoomScale:self.minimumZoomScale animated:YES];
        else
            [self setZoomScale:self.minimumZoomScale animated:YES];
    }
}
 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = imageView.frame;
    
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2;
    else
        frameToCenter.origin.x = 0;
    
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2;
    else
        frameToCenter.origin.y = 0;
    
    imageView.frame = frameToCenter;
}


- (void)setScrollZoom:(bool)flag
{
    if(flag){
        isEditMode = YES;
        [self setScrollEnabled:YES];
    }else{
        isEditMode = NO;
        [self setScrollEnabled:NO];
    }
}

@end
